<div>
	<h2>About</h2>
	This sample simulates names database. 
	From this form you can choose up to 8 names or add new ones to database. 
	Database connection is done using Ajax, JSon and PHP. 
	<br><br>
</div>

{$contents}

{literal}
<div style="text-align:left; padding:10px;">
<h2>Controller method</h2>
This method will be called by ajax when search is requested. It returns Json data as a result.
<pre class="code">
&lt;?php
public function ajaxSearchSelectQuery( $search ) {
	$items = SearchSelectItem::dataQuery()
		-&gt;select('t.id, t.name, t.age')
		-&gt;where('t.name LIKE \'%' . addslashes($search) . '%\'')
		-&gt;orderBy('t.name')
		-&gt;limit(20)
		-&gt;commit()-&gt;toArray();
		
	$json = '[';
	foreach( $items as $item ) {
		$json .= "{";
		$json .= "'key':'" . addslashes($item['id']) . "',";
		$json .= "'value':'" . addslashes($item['name']) . "',";
		$json .= "'showValue':'" . addslashes($item['name'] . '&lt;b&gt; (Age:'.$item['age'].')&lt;/b&gt;') . "',";
		$json .= "},";
	}
	$json .= ']';
	die( $json );
}
?&gt;
</pre>

<h2>Source</h2>

<pre class="code">
&lt;?php
$this-&gt;view-&gt;addPartitioner('contents', 'DomFg/Source/SearchSelect.html');

$post = DomFgPost::getInstance();
$selected  = array(1=&gt;'Oliver');
if( $post-&gt;save ) {
	$items = '';
	foreach( $post-&gt;items as $item ) {
		
		// all items key's that are added as new start with word 'new'.
		if( substr($item['key'],0,3) == 'new' ) {
			
			// check if item with this name already exists. We don't want duplicates
			$newItem = SearchSelectItem::query()-&gt;where('t.name LIKE \''.addslashes($item['value']).'\'')-&gt;limit(1)-&gt;commit();
			if( $newItem ) {
				$items .= '&lt;b&gt;'.$item['value'].'&lt;/b&gt; already existed in database. Its id is: &lt;b&gt;'.$newItem-&gt;id.'&lt;/b&gt;&lt;br&gt;';
			
			// if id didn't, lets create it.
			} else {
				$newItem = new SearchSelectItem();
				$newItem-&gt;name = $item['value'];
				$newItem-&gt;age = mt_rand(20,60);
				$newItem-&gt;save();
				$items .= '&lt;b&gt;'.$item['value'].'&lt;/b&gt; was added to database. Its id is: &lt;b&gt;'.$newItem-&gt;id.'&lt;/b&gt;&lt;br&gt;';
			}
			
			$selected[$newItem-&gt;id] = $newItem-&gt;name;
			
		// item was not added as new. So it should exist in database.
		} else {
			$items .= '&lt;b&gt;'.$item['value'].'&lt;/b&gt; with id &lt;b&gt;'.$item['key'].'&lt;/b&gt;&lt;br&gt;';
			$selected[$item['key']] = $item['value'];
		}
	}
	$message = array(
		'type' =&gt; 'info',
		'colspan' =&gt; 3,
		'message' =&gt; 'You selected these items: ',
		'description' =&gt; $items,
	);
}

$panel = array(
	'title' =&gt; 'Search select',
	'layout' =&gt; 'table',
	'layoutConfig' =&gt; array(
		'columns' =&gt; 3,
	),
	'style' =&gt; 'width:500px',
	'action' =&gt; $this-&gt;getUrl('ajaxSearchSelect'),
	'items' =&gt; array(
		$message,
		array(
			'type' =&gt; 'searchSelect',
			'name' =&gt;  'items',
			'maxElements' =&gt; 8,
			'allowAddingNew' =&gt; true,
			'selected' =&gt; $selected,
			'ajaxUrl' =&gt; $this-&gt;getUrl('ajaxSearchSelectQuery/[$search]'),
		),
		array(
			'type' =&gt; 'button',
			'title' =&gt; 'Save',
			'name' =&gt; 'save',
		),
	),	
);
$this-&gt;view-&gt;contents = DomFg::render( $panel );
?&gt;
</pre>
</div>
{/literal}